home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_ghostscript.idb / usr / freeware / doc / ghostscript / 3.33 / drivers.doc.z / drivers.doc
Encoding:
Text File  |  1998-05-21  |  32.6 KB  |  793 lines

  1.    Copyright (C) 1989, 1995 Aladdin Enterprises.
  2.      All rights reserved.
  3.   
  4.   This file is part of GNU Ghostscript.
  5.   
  6.   GNU Ghostscript is distributed in the hope that it will be useful, but
  7.   WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  8.   anyone for the consequences of using it or for whether it serves any
  9.   particular purpose or works at all, unless he says so in writing.  Refer
  10.   to the GNU Ghostscript General Public License for full details.
  11.   
  12.  
  13. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14.  
  15. This file, drivers.doc, describes the interface between Ghostscript and
  16. device drivers.
  17.  
  18. For an overview of Ghostscript and a list of the documentation files, see
  19. README.
  20.  
  21. ********
  22. ******** Adding a driver ********
  23. ********
  24.  
  25. To add a driver to Ghostscript, all you need to do is edit devs.mak in
  26. two places.  The first is the list of devices, in the section headed
  27.  
  28. # -------------------------------- Catalog ------------------------------- #
  29.  
  30. Pick a name for your device, say smurf, and add smurf to the list.
  31. (Device names must be 1 to 8 characters, consisting of only letters,
  32. digits, and underscores, of which the first character must be a letter.
  33. Case is significant: all current device names are lower case.)
  34. The second is the section headed
  35.  
  36. # ---------------------------- Device drivers ---------------------------- #
  37.  
  38. Suppose the files containing the smurf driver are called joe and fred.
  39. Then you should add the following lines:
  40.  
  41. # ------ The SMURF device ------ #
  42.  
  43. smurf_=joe.$(OBJ) fred.$(OBJ)
  44. smurf.dev: $(smurf_)
  45.     $(SHP)gssetdev smurf $(smurf_)
  46.  
  47. joe.$(OBJ): joe.c ...and whatever it depends on
  48.  
  49. fred.$(OBJ): fred.c ...and whatever it depends on
  50.  
  51. If the smurf driver also needs special libraries, e.g., a library named
  52. gorf, then the gssetdev line should look like
  53.     $(SHP)gssetdev smurf $(smurf_)
  54.     $(SHP)gsaddmod smurf -lib gorf
  55.  
  56. ********
  57. ******** Keeping things simple
  58. ********
  59.  
  60. If you want to add a simple device (specifically, a black-and-white
  61. printer), you probably don't need to read the rest of this document; just
  62. use the code in an existing driver as a guide.  The Epson and BubbleJet
  63. drivers (gdevepsn.c and gdevbj10.c) are good models for dot-matrix printers,
  64. which require presenting the data for many scan lines at once; the
  65. DeskJet/LaserJet drivers (gdevdjet.c) are good models for laser printers,
  66. which take a single scan line at a time but support data compression.  For
  67. color printers, the DeskJet 500C driver (gdevcdj.c) may be a good place to
  68. start, although it is large and complex.  gdevcdj.c is also a good example
  69. of a device that has additional settable attributes (in this case, different
  70. output quality modes that aren't just a matter of resolution).
  71.  
  72. On the other hand, if you're writing a driver for some more esoteric
  73. device, you probably do need at least some of the information in the rest
  74. of this document.  It might be a good idea for you to read it in
  75. conjunction with one of the existing drivers.
  76.  
  77. ********
  78. ******** Driver structure ********
  79. ********
  80.  
  81. A device is represented by a structure divided into three parts:
  82.  
  83.     - procedures that are shared by all instances of each device;
  84.  
  85.     - parameters that are present in all devices but may be different
  86.       for each device or instance; and
  87.  
  88.     - device-specific parameters that may be different for each instance.
  89.  
  90. Normally, the procedure structure is defined and initialized at compile
  91. time.  A prototype of the parameter structure (including both generic and
  92. device-specific parameters) is defined and initialized at compile time,
  93. but is copied and filled in when an instance of the device is created.
  94.  
  95. The gx_device_common macro defines the common structure elements, with the
  96. intent that devices define and export a structure along the following
  97. lines:
  98.  
  99.     typedef struct smurf_device_s {
  100.         gx_device_common;
  101.         ... device-specific parameters ...
  102.     } smurf_device;
  103.     smurf_device gs_smurf_device = {
  104.         sizeof(smurf_device),        /* params_size */
  105.         0,                /* static_procs, obsolete */
  106.         ... generic parameter values ...
  107.         { ... procedures ... },        /* std_procs */
  108.         ... device-specific parameter values ...
  109.     };
  110.  
  111. The device structure instance *must* have the name gs_smurf_device, where
  112. smurf is the device name used in devs.mak.
  113.  
  114. All the device procedures are called with the device as the first
  115. argument.  Since each device type is actually a different structure type,
  116. the device procedures must be declared as taking a gx_device * as their
  117. first argument, and must cast it to smurf_device * internally.  For
  118. example, in the code for the "memory" device, the first argument to all
  119. routines is called dev, but the routines actually use md to reference
  120. elements of the full structure, by virtue of the definition
  121.  
  122.     #define md ((gx_device_memory *)dev)
  123.  
  124. (This is a cheap version of "object-oriented" programming: in C++, for
  125. example, the cast would be unnecessary, and in fact the procedure table
  126. would be constructed by the compiler.)
  127.  
  128. Structure definition
  129. --------------------
  130.  
  131. This essentially duplicates the first part of the structure definition in
  132. gxdevice.h.
  133.  
  134. typedef struct gx_device_s {
  135.     int params_size;        /* size of this structure */
  136.     gx_device_procs *static_procs;    /* (obsolete) */
  137.     const char *dname;        /* the device name */
  138.     int width;            /* width in pixels */
  139.     int height;            /* height in pixels */
  140.     ...
  141.     gx_device_color_info color_info;    /* color information */
  142.     ...
  143.     int is_open;            /* true if device has been opened */
  144. } gx_device;
  145.  
  146. The name in the structure should be the same as the name in devs.mak.
  147.  
  148. gx_device_common is a macro consisting of just the element definitions.
  149.  
  150. For sophisticated developers only
  151. ---------------------------------
  152.  
  153. If for any reason you need to change the definition of the basic device
  154. structure, or add procedures, you must change the following places:
  155.  
  156.     - This document and NEWS (if you want to keep the
  157.         documentation up to date).
  158.     - The definition of gx_device_common and/or the procedures
  159.         in gxdevice.h.
  160.     - Possibly, the default forwarding procedures in gxdevice.h
  161.         and gsdevice.c.
  162.     - The following devices that must have complete (non-defaulted)
  163.         procedure vectors:
  164.         - The null device in gsdevice.c.
  165.         - The command list "device" in gxclist.c.  This is
  166.             not an actual device; it only defines procedures.
  167.         - The "memory" devices in gdevmem.h and gdevm*.c.
  168.     - The procedure record completion routine in gsdevice.c.
  169.     - The clip list accumulation "device" in gxacpath.c.
  170.     - The clipping "devices" in gxcpath.c and gxclip2.c.
  171.     - The hit detection "device" in zupath.c.
  172.     - The generic printer device macros in gdevprn.h.
  173.     - The generic printer device code in gdevprn.c.
  174.     - All the real devices in the standard Ghostscript distribution,
  175.         as listed in devs.mak.  (All of them are supposed to use
  176.         the macros in gxdevice.h or gdevprn.h to initialize their
  177.         state, so you may not have to edit the source code for
  178.         them.)
  179.     - Any other drivers you have that aren't part of the standard
  180.         Ghostscript distribution.
  181.  
  182. You may also have to change the code for gx_default_get_params and/or
  183. gx_default_put_params (in gsdparam.c).
  184.  
  185. Note that if all you are doing is adding optional procedures, you do NOT
  186. have to modify any of the real device drivers listed in devs.mak;
  187. Ghostscript will substitute the default procedures properly.
  188.  
  189. ********
  190. ******** Types and coordinates ********
  191. ********
  192.  
  193. Coordinate system
  194. -----------------
  195.  
  196. Since each driver specifies the initial transformation from user to device
  197. coordinates, the driver can use any coordinate system it wants, as long as
  198. a device coordinate will fit in an int.  (This is only an issue on MS-DOS
  199. systems, where ints are only 16 bits.  User coordinates are represented as
  200. floats.)  Typically the coordinate system will have (0,0) in the upper
  201. left corner, with X increasing to the right and Y increasing toward the
  202. bottom.  This happens to be the coordinate system that all the currently
  203. supported devices use.  However, there is supposed to be nothing in the
  204. rest of Ghostscript that assumes this.
  205.  
  206. Drivers must check (and, if necessary, clip) the coordinate parameters
  207. given to them: they should not assume the coordinates will be in bounds.
  208. The fit_fill and fit_copy macros in gxdevice.h are very helpful in doing
  209. this.
  210.  
  211. Color definition
  212. ----------------
  213.  
  214. Ghostscript represents colors internally as RGB or CMYK values.  In
  215. communicating with devices, however, it assumes that each device has a
  216. palette of colors identified by integers (to be precise, elements of type
  217. gx_color_index).  Drivers may provide a uniformly spaced gray ramp or
  218. color cube for halftoning, or they may do their own color approximation,
  219. or both.
  220.  
  221. The color_info member of the device structure defines the color and
  222. gray-scale capabilities of the device.  Its type is defined as follows:
  223.  
  224. typedef struct gx_device_color_info_s {
  225.     int num_components;        /* 1 = gray only, 3 = RGB, */
  226.                     /* 4 = CMYK */
  227.     int depth;            /* # of bits per pixel */
  228.     gx_color_value max_gray;    /* # of distinct gray levels -1 */
  229.     gx_color_value max_rgb;        /* # of distinct color levels -1 */
  230.                     /* (only relevant if num_comp. > 1) */
  231.     gx_color_value dither_gray;    /* size of gray ramp for halftoning */
  232.     gx_color_value dither_rgb;    /* size of color cube ditto */
  233.                     /* (only relevant if num_comp. > 1) */
  234. } gx_device_color_info;
  235.  
  236. The following macros (in gxdevice.h) provide convenient shorthands for
  237. initializing this structure for ordinary black-and-white or color devices:
  238.  
  239. #define dci_black_and_white ...
  240. #define dci_color(depth,maxv,dither) ...
  241.  
  242. The idea is that a device has a certain number of gray levels (max_gray +1)
  243. and a certain number of colors (max_rgb +1) that it can produce directly.
  244. When Ghostscript wants to render a given RGB or CMYK color as a device
  245. color, it first tests whether the color is a gray level.  (If
  246. num_components is 1, it converts all colors to gray levels.)  If so:
  247.  
  248.     - If max_gray is large (>= 31), Ghostscript asks the device to
  249. approximate the gray level directly.  If the device returns a valid
  250. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  251. the device can represent dither_gray distinct gray levels, equally spaced
  252. along the diagonal of the color cube, and uses the two nearest ones to the
  253. desired color for halftoning.
  254.  
  255. If the color is not a gray level:
  256.  
  257.     - If max_rgb is large (>= 31), Ghostscript asks the device to
  258. approximate the color directly.  If the device returns a valid
  259. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  260. the device can represent dither_rgb * dither_rgb * dither_rgb distinct
  261. colors, equally spaced throughout the color cube, and uses two of the
  262. nearest ones to the desired color for halftoning.
  263.  
  264. Types
  265. -----
  266.  
  267. Here is a brief explanation of the various types that appear as parameters
  268. or results of the drivers.
  269.  
  270. gx_color_value (defined in gxdevice.h)
  271.  
  272.     This is the type used to represent RGB or CMYK color values.  It is
  273. currently equivalent to unsigned short.  However, Ghostscript may use less
  274. than the full range of the type to represent color values:
  275. gx_color_value_bits is the number of bits actually used, and
  276. gx_max_color_value is the maximum value (equal to 2^gx_max_color_value_bits
  277. - 1).
  278.  
  279. gx_device (defined in gxdevice.h)
  280.  
  281.     This is the device structure, as explained above.
  282.  
  283. gs_matrix (defined in gsmatrix.h)
  284.  
  285.     This is a 2-D homogenous coordinate transformation matrix, used by
  286. many Ghostscript operators.
  287.  
  288. gx_color_index (defined in gxdevice.h)
  289.  
  290.     This is meant to be whatever the driver uses to represent a device
  291. color.  For example, it might be an index in a color map, or it might be
  292. R, G, and B values packed into a single integer.  Ghostscript doesn't ever
  293. do any computations with gx_color_index values: it gets them from
  294. map_rgb_color or map_cmyk_color and hands them back as arguments to
  295. several other procedures.  The special value gx_no_color_index (defined as
  296. (gx_color_index)(-1)) means "transparent" for some of the procedures.  The
  297. type definition is simply:
  298.  
  299.     typedef unsigned long gx_color_index;
  300.  
  301. gs_param_list (defined in gsparam.h)
  302.  
  303.     This is a parameter list, which is used to read and set attributes
  304. in a device.  See the comments in gsparam.h, and the description of the
  305. get_params and put_params procedures below, for more detail.
  306.  
  307. gx_bitmap (defined in gxbitmap.h)
  308.  
  309.     This structure type represents a bitmap to be used as a tile for
  310. filling a region (rectangle).  Here is a copy of the relevant part of the
  311. file:
  312.  
  313. /*
  314.  * Structure for describing stored bitmaps.
  315.  * Bitmaps are stored bit-big-endian (i.e., the 2^7 bit of the first
  316.  * byte corresponds to x=0), as a sequence of bytes (i.e., you can't
  317.  * do word-oriented operations on them if you're on a little-endian
  318.  * platform like the Intel 80x86 or VAX).  Each scan line must start on
  319.  * a (32-bit) word boundary, and hence is padded to a word boundary,
  320.  * although this should rarely be of concern, since the raster and width
  321.  * are specified individually.  The first scan line corresponds to y=0
  322.  * in whatever coordinate system is relevant.
  323.  *
  324.  * For bitmaps used as halftone tiles, we may replicate the tile in
  325.  * X and/or Y, but it is still valuable to know the true tile dimensions.
  326.  */
  327. typedef struct gx_bitmap_s {
  328.     byte *data;
  329.     int raster;            /* bytes per scan line */
  330.     gs_int_point size;        /* width, height */
  331.     gx_bitmap_id id;
  332.     ushort rep_width, rep_height;    /* true size of tile */
  333. } gx_bitmap;
  334.  
  335. ********
  336. ******** Coding conventions ********
  337. ********
  338.  
  339. While most drivers (especially printer drivers) follow a very similar
  340. template, there is one important coding convention that is not obvious from
  341. reading the code for existing drivers: Driver procedures must not use
  342. malloc to allocate any storage that stays around after the procedure
  343. returns.  Instead, they must use gs_malloc and gs_free, which have slightly
  344. different calling conventions.  (The prototypes for these are in
  345. gsmemory.h, which is included in gx.h, which is included in gdevprn.h.)
  346. This is necessary so that Ghostscript can clean up all allocated memory
  347. before exiting, which is essential in environments that provide only
  348. single-address-space multi-tasking (specifically, Microsoft Windows).
  349.  
  350. char *gs_malloc(uint num_elements, uint element_size,
  351.   const char *client_name);
  352.  
  353.     Like calloc, but unlike malloc, gs_malloc takes an element count
  354. and an element size.  For structures, num_elements is 1 and element_size is
  355. sizeof the structure; for byte arrays, num_elements is the number of bytes
  356. and element_size is 1.  Unlike calloc, gs_malloc does NOT clear the block
  357. of storage.
  358.  
  359.     The client_name is used for tracing and debugging.  It must be a
  360. real string, not NULL.  Normally it is the name of the procedure in which
  361. the call occurs.
  362.  
  363. void gs_free(char *data, uint num_elements, uint element_size,
  364.   const char *client_name);
  365.  
  366.     Unlike free, gs_free demands that num_elements and element_size be
  367. supplied.  It also requires a client name, like gs_malloc.
  368.  
  369. All the driver procedures defined below that return int results return 0 on
  370. success, or an appropriate negative error code in the case of error
  371. conditions.  The error codes are defined in gserrors.h.  The relevant ones
  372. for drivers are as follows:
  373.  
  374.     gs_error_invalidfileaccess
  375.         An attempt to open a file failed.
  376.  
  377.     gs_error_limitcheck
  378.         An otherwise valid parameter value was too large for
  379.         the implementation.
  380.  
  381.     gs_error_rangecheck
  382.         A parameter was outside the valid range.
  383.  
  384.     gs_error_VMerror
  385.         An attempt to allocate memory failed.  (If this
  386.         happens, the procedure should release all memory it
  387.         allocated before it returns.)
  388.  
  389. If a driver does return an error, it should use the return_error
  390. macro rather than a simple return statement, e.g.,
  391.  
  392.     return_error(gs_error_VMerror);
  393.  
  394. This macro is defined in gx.h, which is automatically included by
  395. gdevprn.h but not by gserrors.h.
  396.  
  397. ********
  398. ******** Printer drivers ********
  399. ********
  400.  
  401. Printer drivers (which include drivers that write some kind of raster
  402. file) are especially simple to implement.  Of the driver procedures
  403. defined in the next section, they only need implement two: map_rgb_color
  404. (or map_cmyk_color) and map_color_rgb.  In addition, they must implement a
  405. print_page or print_page_copies procedure.  There are a set of macros in
  406. gdevprn.h that generate the device structure for such devices, of which
  407. the simplest is prn_device; for an example, see gdevbj10.c.  If you are
  408. writing a printer driver, we suggest you start by reading gdevprn.h and
  409. the subsection on "Color mapping" below; you may be able to ignore all the
  410. rest of the driver procedures.
  411.  
  412. The print_page procedures are defined as follows:
  413.  
  414. int (*print_page)(P2(gx_device_printer *, FILE *))
  415. int (*print_page_copies)(P3(gx_device_printer *, FILE *, int))
  416.  
  417.     This procedure must read out the rendered image from the device and
  418. write whatever is appropriate to the file.  To read back one or more scan
  419. lines of the image, the print_page procedure must call one of the following
  420. procedures:
  421.  
  422.     int gdev_prn_copy_scan_lines(P4(gx_device_printer *pdev, int y, byte *str,
  423.       uint size)
  424.  
  425. For this procedure, str is where the data should be copied to, and size is
  426. the size of the buffer starting at str.  This procedure returns the number
  427. of scan lines copied, or <0 for an error.
  428.  
  429.     int gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str,
  430.       byte **actual_data)
  431.  
  432. This procedure reads out exactly one scan line.  If the scan line is
  433. available in the correct format already, *actual_data is set to point to
  434. it; otherwise, the scan line is copied to the buffer starting at str, and
  435. *actual_data is set to str.  This saves a copying step most of the time.
  436.  
  437. In either case, each row of the image is stored in the form described in
  438. the comment under gx_bitmap above; each pixel takes the number of bits
  439. specified as color_info.depth in the device structure, and holds values
  440. returned by the device's map_{rgb,cmyk}_color procedure.
  441.  
  442. The print_page procedure can determine the number of bytes required to hold
  443. a scan line by calling:
  444.  
  445.     uint gdev_prn_raster(P1(gx_device_printer *))
  446.  
  447. For a very simple concrete example, we suggest reading the code in
  448. bit_print_page in gdevbit.c.
  449.  
  450. If the device provides print_page, Ghostscript will call print_page the
  451. requisite number of times to print the desired number of copies; if the
  452. device provides print_page_copies, Ghostscript will call print_page_copies
  453. once per page, passing it the desired number of copies.
  454.  
  455. ********
  456. ******** Driver procedures ********
  457. ********
  458.  
  459. Most of the procedures that a driver may implement are optional.  If a
  460. device doesn't supply an optional procedure <proc>, the entry in the
  461. procedure structure may be either gx_default_<proc>, e.g.
  462. gx_default_tile_rectangle, or NULL or 0.  (The device procedure must also
  463. call the gx_default_ procedure if it doesn't implement the function for
  464. particular values of the arguments.)  Since C compilers supply 0 as the
  465. value for omitted structure elements, this convention means that
  466. statically initialized procedure structures will continue to work even if
  467. new (optional) members are added.
  468.  
  469. Life cycle
  470. ----------
  471.  
  472. A device instance start out life in a closed state.  In this state, no
  473. output operations will occur.  Only the following procedures may be called:
  474.     open_device
  475.     get_initial_matrix
  476.     get_params
  477.     put_params
  478.  
  479. When setdevice installs a device instance in the graphics state, it checks
  480. whether the instance is closed or open.  If the instance is closed,
  481. setdevice calls the open routine, and then sets the state to open.
  482.  
  483. There is currently no user-accessible operation to close a device instance.
  484. Device instances are only closed when they are about to be freed, which
  485. occurs in three situations:
  486.  
  487.     - When a 'restore' occurs, if the instance was created since the
  488. corresponding 'save';
  489.  
  490.     - By the garbage collector, if the instance is no longer accessible;
  491.  
  492.     - When Ghostscript exits (terminates).
  493.  
  494. Open/close/sync
  495. ---------------
  496.  
  497. int (*open_device)(P1(gx_device *)) [OPTIONAL]
  498.  
  499.     Open the device: do any initialization associated with making the
  500. device instance valid.  This must be done before any output to the device.
  501. The default implementation does nothing.
  502.  
  503. void (*get_initial_matrix)(P2(gx_device *, gs_matrix *)) [OPTIONAL]
  504.  
  505.     Construct the initial transformation matrix mapping user
  506. coordinates (nominally 1/72" per unit) to device coordinates.  The default
  507. procedure computes this from width, height, and x/y_pixels_per_inch on the
  508. assumption that the origin is in the upper left corner, i.e.
  509.         xx = x_pixels_per_inch/72, xy = 0,
  510.         yx = 0, yy = -y_pixels_per_inch/72,
  511.         tx = 0, ty = height.
  512.  
  513. int (*sync_output)(P1(gx_device *)) [OPTIONAL]
  514.  
  515.     Synchronize the device.  If any output to the device has been
  516. buffered, send / write it now.  Note that this may be called several times
  517. in the process of constructing a page, so printer drivers should NOT
  518. implement this by printing the page.  The default implementation does
  519. nothing.
  520.  
  521. int (*output_page)(P3(gx_device *, int num_copies, int flush)) [OPTIONAL]
  522.  
  523.     Output a fully composed page to the device.  The num_copies
  524. argument is the number of copies that should be produced for a hardcopy
  525. device.  (This may be ignored if the driver has some other way to specify
  526. the number of copies.)  The flush argument is true for showpage, false for
  527. copypage.  The default definition just calls sync_output.  Printer drivers
  528. should implement this by printing and ejecting the page.
  529.  
  530. int (*close_device)(P1(gx_device *)) [OPTIONAL]
  531.  
  532.     Close the device: release any associated resources.  After this,
  533. output to the device is no longer allowed.  The default implementation
  534. does nothing.
  535.  
  536. Color/alpha mapping
  537. -------------------
  538.  
  539. A given driver normally will implement either map_rgb_color or
  540. map_cmyk_color, but not both.  Black-and-white drivers do not need to
  541. implement either one.
  542.  
  543. gx_color_index (*map_rgb_color)(P4(gx_device *, gx_color_value red,
  544.   gx_color_value green, gx_color_value blue)) [OPTIONAL]
  545.  
  546.     Map a RGB color to a device color.  The range of legal values of
  547. the RGB arguments is 0 to gx_max_color_value.  The default algorithm uses
  548. the map_cmyk_color procedure if the driver supplies one, otherwise returns
  549. 1 if any of the values exceeds gx_max_color_value/2, 0 otherwise.
  550.  
  551.     Ghostscript assumes that for devices that have color capability
  552. (i.e., color_info.num_components > 1), map_rgb_color returns a color index
  553. for a gray level (as opposed to a non-gray color) iff red = green = blue.
  554.  
  555. gx_color_index (*map_cmyk_color)(P5(gx_device *, gx_color_value cyan,
  556.   gx_color_value magenta, gx_color_value yellow, gx_color_value black))
  557.   [OPTIONAL]
  558.  
  559.     Map a CMYK color to a device color.  The range of legal values of
  560. the CMYK arguments is 0 to gx_max_color_value.  The default algorithm
  561. calls the map_rgb_color procedure, with suitably transformed arguments.
  562.  
  563.     Ghostscript assumes that for devices that have color capability
  564. (i.e., color_info.num_components > 1), map_cmyk_color returns a color
  565. index for a gray level (as opposed to a non-gray color) iff cyan = magenta
  566. = yellow.
  567.  
  568. int (*map_color_rgb)(P3(gx_device *, gx_color_index color,
  569.   gx_color_value rgb[3])) [OPTIONAL]
  570.  
  571.     Map a device color code to RGB values.  The default algorithm
  572. returns (0 if color==0 else gx_max_color_value) for all three components.
  573.  
  574. gx_color_index (*map_rgb_alpha_color)(P5(gx_device *, gx_color_value red,
  575.   gx_color_value green, gx_color_value blue, gx_color_value alpha)) [OPTIONAL]
  576.  
  577.     Map a RGB color and an opacity value to a device color.  The range
  578. of legal values of the RGB and alpha arguments is 0 to gx_max_color_value;
  579. alpha = 0 means transparent, alpha = gx_max_color_value means fully
  580. opaque.  The default is to use the map_rgb_color procedure and ignore
  581. alpha.
  582.  
  583.     Note that if a driver implements map_rgb_alpha_color, it must also
  584. implement map_rgb_color, and must implement them in such a way that
  585. map_rgb_alpha_color(dev, r, g, b, gx_max_color_value) returns the same
  586. value as map_rgb_color(dev, r, g, b).
  587.  
  588.     Note that there is no map_cmyk_alpha_color procedure.  CMYK
  589. devices currently do not support variable opacity; alpha is ignored on
  590. such devices.
  591.  
  592. typedef enum { go_text, go_graphics } graphic_object_type;
  593. int (*get_alpha_bits)(P4(gx_device *dev, graphic_object_type type)) [OPTIONAL]
  594.  
  595.     Return the number of alpha (opacity) bits that should be used in
  596. rendering an object of the given type.  The default value is 1.
  597.  
  598. Drawing
  599. -------
  600.  
  601. All drawing operations use device coordinates and device color values.
  602.  
  603. int (*fill_rectangle)(P6(gx_device *, int x, int y,
  604.   int width, int height, gx_color_index color))
  605.  
  606.     Fill a rectangle with a color.  The set of pixels filled is
  607. {(px,py) | x <= px < x + width and y <= py < y + height}.  In other words,
  608. the point (x,y) is included in the rectangle, as are (x+w-1,y), (x,y+h-1),
  609. and (x+w-1,y+h-1), but *not* (x+w,y), (x,y+h), or (x+w,y+h).  If width <=
  610. 0 or height <= 0, fill_rectangle should return 0 without drawing anything.
  611.  
  612. int (*draw_line)(P6(gx_device *, int x0, int y0, int x1, int y1,
  613.   gx_color_index color)) [OPTIONAL]
  614.  
  615.     Draw a minimum-thickness line from (x0,y0) to (x1,y1).  The
  616. precise set of points to be filled is defined as follows.  First, if y1 <
  617. y0, swap (x0,y0) and (x1,y1).  Then the line includes the point (x0,y0)
  618. but not the point (x1,y1).  If x0=x1 and y0=y1, draw_line should return 0
  619. without drawing anything.
  620.  
  621. Bitmap imaging
  622. --------------
  623.  
  624. Bitmap (or pixmap) images are stored in memory in a nearly standard way.
  625. The first byte corresponds to (0,0) in the image coordinate system: bits
  626. (or polybit color values) are packed into it left-to-right.  There may be
  627. padding at the end of each scan line: the distance from one scan line to
  628. the next is always passed as an explicit argument.
  629.  
  630. int (*copy_mono)(P11(gx_device *, const unsigned char *data, int data_x,
  631.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  632.   gx_color_index color0, gx_color_index color1))
  633.  
  634.     Copy a monochrome image (similar to the PostScript image
  635. operator).  Each scan line is raster bytes wide.  Copying begins at
  636. (data_x,0) and transfers a rectangle of the given width at height to the
  637. device at device coordinate (x,y).  (If the transfer should start at some
  638. non-zero y value in the data, the caller can adjust the data address by
  639. the appropriate multiple of the raster.)  The copying operation writes
  640. device color color0 at each 0-bit, and color1 at each 1-bit: if color0 or
  641. color1 is gx_no_color_index, the device pixel is unaffected if the image
  642. bit is 0 or 1 respectively.  If id is different from gx_no_bitmap_id, it
  643. identifies the bitmap contents unambiguously; a call with the same id will
  644. always have the same data, raster, and data contents.
  645.  
  646.     This operation, with color0 = gx_no_color_index, is the workhorse
  647. for text display in Ghostscript, so implementing it efficiently is very
  648. important.
  649.  
  650. int (*tile_rectangle)(P10(gx_device *, const gx_bitmap *tile,
  651.   int x, int y, int width, int height,
  652.   gx_color_index color0, gx_color_index color1,
  653.   int phase_x, int phase_y)) [OPTIONAL]
  654.  
  655.     Tile a rectangle.  Tiling consists of doing multiple copy_mono
  656. operations to fill the rectangle with copies of the tile.  The tiles are
  657. aligned with the device coordinate system, to avoid "seams".
  658. Specifically, the (phase_x, phase_y) point of the tile is aligned with the
  659. origin of the device coordinate system.  (Note that this is backwards from
  660. the PostScript definition of halftone phase.)  phase_x and phase_y are
  661. guaranteed to be in the range [0..tile->width) and [0..tile->height)
  662. respectively.
  663.  
  664.     If color0 and color1 are both gx_no_color_index, then the tile is
  665. a color pixmap, not a bitmap: see the next section.
  666.  
  667.     This operation is the workhorse for halftone filling in
  668. Ghostscript, so implementing it efficiently for solid tiles (where either
  669. color0 and color1 are both gx_no_color_index, for colored halftones, or
  670. neither one is gx_no_color_index, for monochrome halftones) is very
  671. important.
  672.  
  673. Pixmap imaging
  674. --------------
  675.  
  676. Pixmaps are just like bitmaps, except that each pixel occupies more than
  677. one bit.  All the bits for each pixel are grouped together (this is
  678. sometimes called "chunky" or "Z" format).  For copy_color, the number of
  679. bits per pixel is given by the color_info.depth parameter in the device
  680. structure: the legal values are 1, 2, 4, 8, 16, 24, or 32.  The pixel
  681. values are device color codes (i.e., whatever it is that map_rgb_color
  682. returns).
  683.  
  684. int (*copy_color)(P9(gx_device *, const unsigned char *data, int data_x,
  685.   int raster, gx_bitmap_id id, int x, int y, int width, int height))
  686.  
  687.     Copy a color image with multiple bits per pixel.  The raster is in
  688. bytes, but x and width are in pixels, not bits.  If the device doesn't
  689. actually support color, this is OPTIONAL; the default is equivalent to
  690. copy_mono with color0 = 0 and color1 = 1.  If id is different from
  691. gx_no_bitmap_id, it identifies the bitmap contents unambiguously; a call
  692. with the same id will always have the same data, raster, and data
  693. contents.
  694.  
  695. We do not provide a separate procedure for tiling with a pixmap; instead,
  696. tile_rectangle can also take colored tiles.  This is indicated by the
  697. color0 and color1 arguments both being gx_no_color_index.  In this case,
  698. as for copy_color, the raster and height in the "bitmap" are interpreted
  699. as for real bitmaps, but the x and width are in pixels, not bits.
  700.  
  701. int (*copy_alpha)(P11(gx_device *dev, const unsigned char *data, int data_x,
  702.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  703.   gx_color_index color, int depth)) [OPTIONAL]
  704.  
  705.     Fill a given region with a given color modified by an individual
  706. alpha value for each pixel.  depth, the number of bits per pixel, is
  707. either 2 or 4, and in any case is always a value returned by a previous
  708. call on the get_alpha_bits procedure.  Note that if get_alpha_bits always
  709. returns 1, this procedure will never be called.
  710.  
  711. Reading bits back
  712. -----------------
  713.  
  714. int (*get_bits)(P4(gx_device *, int y, byte *str, byte **actual_data))
  715.   [OPTIONAL]
  716.  
  717.     Read one scan line of bits back from the device into the area
  718. starting at str, namely, scan line y.  If the bits cannot be read back
  719. (e.g., from a printer), return -1; otherwise return 0.  The contents of
  720. the bits beyond the last valid bit in the scan line (as defined by the
  721. device width) are unpredictable.
  722.  
  723.     If actual_data is NULL, the bits are always returned at str.  If
  724. actual_data is not NULL, get_bits may either copy the bits to str and set
  725. *actual_data = str, or it may leave the bits where they are and return a
  726. pointer to them in *actual_data.  In the latter case, the bits are
  727. guaranteed to start on a 32-bit boundary and to be padded to a multiple of
  728. 32 bits; also in this case, the bits are not guaranteed to still be there
  729. after the next call on get_bits.
  730.  
  731. Parameters
  732. ----------
  733.  
  734. Devices may have an open-ended set of parameters, which are simply pairs
  735. consisting of a name and a value.  The value may be of various types:
  736. integer (int or long), boolean, float, string, name, null, array of integer,
  737. or array of float.  For example, the Name of a device is a string; the
  738. Margins of a device is an array of 2 floats.  See gsparam.h for more
  739. details.
  740.  
  741. If a device has parameters other than the ones applicable to all devices
  742. (or, in the case of printer devices, all printer devices), it must provide
  743. get_params and put_params procedures.  If your device has parameters beyond
  744. those of a straightforward display or printer, we strongly advise using the
  745. _get_params and _put_params procedures in an existing device (for example,
  746. gdevcdj.c or gdevbit.c) as a model for your own code.
  747.  
  748. int (*get_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  749.  
  750.     Read the parameters of the device into the parameter list at plist,
  751. using the param_write_* macros/procedures defined in gsparam.h.
  752.     
  753. int (*put_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  754.  
  755.     Set the parameters of the device from the parameter list at plist,
  756. using the param_read_* macros/procedures defined in gsparam.h.  All
  757. put_params procedures must use a "two-phase commit" algorithm; see gsparam.h
  758. for details.
  759.  
  760. External fonts
  761. --------------
  762.  
  763. Drivers may include the ability to display text.  More precisely, they may
  764. supply a set of procedures that in turn implement some font and text
  765. handling capabilities.  These procedures are documented in another file,
  766. xfonts.doc.  The link between the two is the driver procedure that
  767. supplies the font/text procedures:
  768.  
  769. xfont_procs *(*get_xfont_procs)(P1(gx_device *dev)) [OPTIONAL]
  770.  
  771.     Return a structure of procedures for handling external fonts and
  772. text display.  A NULL value means that this driver doesn't provide this
  773. capability.
  774.  
  775. For technical reasons, a second procedure is also needed:
  776.  
  777. gx_device *(*get_xfont_device)(P1(gx_device *dev)) [OPTIONAL]
  778.  
  779.     Return the device that implements get_xfont_procs in a non-default
  780. way for this device, if any.  Except for certain special internal devices,
  781. this is always the device argument.
  782.  
  783. Page devices
  784. ------------
  785.  
  786. gx_device (*get_page_device)(P1(gx_device *dev)) [OPTIONAL]
  787.  
  788.     According to the Adobe specifications, some devices are "page
  789. devices" and some are not.  This procedure returns NULL if the device is
  790. not a page device, or the device itself if it is a page device.  In the
  791. case of forwarding devices, get_page_device returns the underlying page
  792. device (or NULL if the underlying device is not a page device).
  793.